<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<body>
<!--<form action="login.html"> 跳不转，得把input类型改为submit，并使onsubmit="return check()"为true才能跳转-->
<form action="login.html" onsubmit="return insertUser()">
    用户名：<input type="text" name="user" id="user" /><br />
    密码：<input type="password" name="password" id="password"/><br />
    <input type="submit" value="登录"/><input type="reset" value="重置" />
</form>
<button onclick="window.location.href = 'login.html'">返回登录页</button>
<script src="jquery-3.3.1.min.js"></script>
<script>
    function insertUser(){
		var user=$("#user").val();
		var password=$("#password").val();
		var a=1;
		$.ajax({
			type: "post",//传递方法
			url: "/login/insert",//数据接口
			data:JSON.stringify({user:user,password:password}),
			contentType: "application/json",
			success: function(msg)//如果接收成功执行以下
			{
				if(msg=="faild"){
				    alert("用户名已存在，请另起用户名")
				}
				else if(msg=="success"){
				    alert("注册成功")
				    a=2;
				}
				console.log(msg);
			},
			error:function()//如果接收不成功执行以下
			{
				console.log("请求失败");
			}
		});
		if(a==2){
		    return true;
		}
		return false;
	}
</script>
</body>

</html>